// JavaScript Document
$(document).ready(function() {
  //alert(document.cookie);
  var category = Get_Cookie('category');

  if(category){
    $('#edit-st-search-category').val(category);
    category = '';
    document.cookie = "category="+category+"; path=/";
  }
  else{
    $('#edit-st-search-category').val('Choose Category');
  }

  var module_alert = $("#module").val(); //name of the module/functionality to display in alert box
  $("#srt_insertDate").addClass("sortDsc");
  $("#hid_sort").val("descending,insertDate");

					   
  //Date picker for date search in message listing pages
  $('#edit-news-search-date').datepicker({
    dateFormat:'yy-mm-dd'
  });
  $('#edit-st-search-date').datepicker({
    dateFormat:'yy-mm-dd'
  });
  $('#edit-ct-search-date').datepicker({
    dateFormat:'yy-mm-dd'
  });

   
  //Function to activate stores/categories
  $("#edit-activate").click(function() {

    var checkbox_values = [];
    var  alert_mess;
    $('input[@name=checkBoxList]:checked').each(function(i){
      checkbox_values[i] = $(this).val();//store the selected values in an array
    });

    if (module_alert == "stores") {
      alert_mess = "store";
    }
    if (module_alert == "categories") {
      alert_mess = "category";
    }

    if(checkbox_values.length == 0){
      $('#dialog').dialog('destroy');
      openAlertDialog(400, 150, 'Information', 'Please select atleast one ' + alert_mess + ' to activate','information');
      return false;
    }

    var n =0;
    for(i=0;i<checkbox_values.length;i++){
      strcategories = $('#status'+checkbox_values[i]).html();
      n = strcategories.search('Active');
      if(n >= 0 ){
        $('#dialog').dialog('destroy');
        openAlertDialog(400, 150, 'Information', 'Please select the ' + alert_mess + ' which is in Inactive status','information');
        return false;
      }
    }

    activateItems(checkbox_values,module_alert);
    return false;
  });
 

  //Function to deactivate stores/categories
  $("#edit-deactivate").click(function() {
    var checkbox_values = [];
    var alert_mess;
    $('input[@name=checkBoxList]:checked').each(function(i){
      checkbox_values[i] = $(this).val();//store the selected values in an array
    });

    if (module_alert == "stores") {
      alert_mess = "store";
    }

    if (module_alert == "categories") {
      alert_mess = "category";
    }
    if(checkbox_values.length == 0){
      $('#dialog').dialog('destroy');
      openAlertDialog(400, 150, 'Information', 'Please select atleast one ' + alert_mess + ' to deactivate','information');
      return false;
    }

    var n =0;
    for(i=0;i<checkbox_values.length;i++){
      strstores = $('#status'+checkbox_values[i]).html();
      n = strstores.search('Inactive');
      if(n >= 0 ){
        $('#dialog').dialog('destroy');
        openAlertDialog(400, 150, 'Information', 'Please select the '+ alert_mess +' which is in Active status','information');
        return false;
      }
    }
    deactivateItems(checkbox_values,module_alert);
    return false;
  });
 
});

function unselect(){
  if($('input[name=checkAll]').is(':checked')){
    $('input[name=checkAll]').attr('checked', false);
  }
}

function showstates()
{
  // if($("#edit-find-country").val() == "United States"){
  if($("#edit-find-country").val() == "US"){
    $("#usstates").show();
    $("#otherstates").hide();
  }
  else{
    $("#usstates").hide();
    $("#otherstates").show();
  }
}

function deactivateItems(checkbox_values,module_alert){
  var pageno   = $('#hid_pageno').val();
  var module   = $('#module').val();
  if(module_alert == "categories"){
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');

      $.ajax({
        type: "POST",
        url : baseurl+'findastore/categoryactivation/Deactivate',
        data: '&catvalues=' + checkbox_values,
        success: function(res){
          eval("var json="+res);
					
          if(json.Message == 'Success'){
            //applyCategoriesFilterAction(pageno,module);
            //$('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Information', 'Selected Categories are deactivated successfully','success');
            $('input[name=checkAll]').attr('checked', false);
          }
          else{
            $('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Error', 'Error in deactivating the categories');
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },300,100,"Confirm Deactivate","Do you want to Deactivate?");
    return false;
  }
  else{
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
      $.ajax({
        type: "POST",
        url : baseurl+'findastore/storeactivation/Deactivate',
        data: '&storevalues=' + checkbox_values,
        success: function(res){
          eval("var json="+res);					
          if(json.Message == 'Success'){
            $('input[name=checkAll]').attr('checked', false);
            applyStoresFilterAction(pageno,module);
            $('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Information', 'Selected Stores are Deactivated successfully','success');
          }
          else{
            $('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Error', 'Error in deactivating the stores');
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },300,100,"Confirm Deactivate","Do you want to Deactivate?");
    return false;
  }
}


function activateItems(checkbox_values,module_alert){ 
  var pageno   = $('#hid_pageno').val();
  var module   = $('#module').val();
  if(module_alert == "categories"){
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');

      $.ajax({
        type: "POST",
        url : baseurl+'findastore/categoryactivation/Activate',
        data: '&catvalues=' + checkbox_values,
        success: function(res){
          eval("var json="+res);
					                      
          if(json.Message == 'Success'){
            applyCategoriesFilterAction(pageno,module);
            $('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Information', 'Selected Categories are activated successfully','success');
            $('input[name=checkAll]').attr('checked', false);
          }
          else{
            reopen_dialog(300,125,"Error");
            $('#dialog').dialog('destroy');
            openAlertDialog(300, 150, 'Error', 'Error in activating the categories');
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },300,100,"Confirm Activate","Do you want to Activate?");
    return false;
  }

	

  else{
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
      $.ajax({
        type: "POST",
        url : baseurl+'findastore/storeactivation/Activate',
        data: '&storevalues=' + checkbox_values,
        success: function(res){
          eval("var json="+res);
					 
          if(json.Message == 'Success'){
            applyStoresFilterAction(pageno,module);
            $('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Information', 'Selected Stores are activated successfully','success');
            $('input[name=checkAll]').attr('checked', false);
          }
          else{
            $('#dialog').dialog('destroy');
            openAlertDialog(350, 150, 'Error', 'Error in activating the stores');
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },300,100,"Confirm Activate","Do you want to Activate?");
    return false;
  }
}


function addStore(){  
  openDialog("addstore", 0, 680, 470, "Add Store");
  
}


function add_store(catid){
  categoryname = $('#category_name').html();
  reopen_dialog(680, 470, "Add Store");
  storesArray["category_name"] = categoryname;

  $.ajax({
    type : "GET",
    url : baseurl+'findastore/addstore/'+categoryname+'/'+catid,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $("#edit-find-store-name").focus();
      $('#edit-find-category').val(catid);
    /*
		  $('#set_timings_mon').hide();
		  $('#set_timings_tue').hide();
		  $('#set_timings_wed').hide();
		  $('#set_timings_thu').hide();
		  $('#set_timings_fri').hide();
		  $('#set_timings_sat').hide();
		  $('#set_timings_sun').hide();
		  */
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function addStoredialog(){    
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/addstore',
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      /*
          $("#dialog").keyup(function(e) {
			if(e.keyCode == 13) {		
			  saveStore(-1);
			}
          }); 
		  */

      $("#edit-find-store-name").focus();

    /*
		  $('#set_timings_mon').hide();
		  $('#set_timings_tue').hide();
		  $('#set_timings_wed').hide();
		  $('#set_timings_thu').hide();
		  $('#set_timings_fri').hide();
		  $('#set_timings_sat').hide();
		  $('#set_timings_sun').hide();
		  */

    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

//Function to disable the timeselection if the user selects 24 Hours
function disableTimeSelect () {
  if ($("#edit-24-hrs").attr("checked") == true) {
    $("#edit-opening-time-hr").attr("disabled", true);
    $("#edit-opening-time-min").attr("disabled", true);
    $("#edit-closing-time-hr").attr("disabled", true);
    $("#edit-closing-time-min").attr("disabled", true);
  }
  else {
    $("#edit-opening-time-hr").attr("disabled", false);
    $("#edit-opening-time-min").attr("disabled", false);
    $("#edit-closing-time-hr").attr("disabled", false);
    $("#edit-closing-time-min").attr("disabled", false);
  }
}

function saveStore(catid,catname){   
  $("#alertdialog").dialog("destroy");
  var storename   =  $('#edit-find-store-name').val();
  var category    =  $('#edit-find-category').val();
  var address1    =  $('#edit-find-address1').val();
  var address2    =  $('#edit-find-address2').val();
  var zipcode     =  $('#edit-find-zipcode').val();
  var state       =  $('#edit-find-state').val();
  var usstates    =  $('#edit-find-usstates').val();
  var city        =  $('#edit-find-city').val();
  var country     =  $('#edit-find-country').val();
  var phone       =  $('#edit-work-phone').val();
  var phone2      =  $('#edit-work-phone-additional').val();
  var selected = $("#edit-find-country option:selected");
  if(selected.val() != -1)
  {
    var countryname=selected.text();
 
  }

  var status      =  $('#edit-find-status').val();
	
  var phonetype   =  $('#edit-work-phone-type').val();
  var phonetype_additional   =  $('#edit-work-phone-additional-type').val();
  var storehours    = $('#edit-storehours').val();

  //code to validate the fields
  var lines = 10;
  var str = "";
  if ($.trim(storename) == "") {
    str += "Please enter the Store name";
    lines+=18;
  }
	
  if ($.trim(storename) != "") {
    if(storename.length > 50) {
      if(str!="") str+= "<br/>";
      str += "Store name should not be more than 50 characters";
      lines+=18;
    }
  }
  if((category == null)||(category == "")  || (category == -1)) {
    if(str!="") str+= "<br/>";
    str += "Please select atleast one Category";
    lines+=18;
  }

  if(country == -1) {
    if(str!="") str+= "<br/>";
    str += "Please select any Country";
    lines+=18;
  }


  if($.trim(address1) == "") {
    if(str!="") str+= "<br/>";
    str += "Please enter the Address Line1";
    lines+=18;
  }

        

  if($.trim(city) == "") {
    if(str!="") str+= "<br/>";
    str += "Please enter the City";
    lines+=18;
  }
        

  //if($.trim(country) == "United States") {
  if($.trim(country) == "US") {
    if(usstates == "")
    {
      if(str!="") str+= "<br/>";
      str += "Please select the State/Province";
      lines+=18;
    }
  }
  //  if($.trim(country) != "United States") {
  if($.trim(country) != "US") {
    if($.trim(state) == "")
    {
      if(str!="") str+= "<br/>";
      str += "Please enter the State/Province";
      lines+=18;
    }
  }

	
  if($.trim(zipcode) == "") {
    if(str!="") str+= "<br/>";
    str += "Please enter the Postal Code";
    lines+=18;
  }
  
	
  /*if (invalid_address == 1) {
    if(str!="") str+= "<br/>";
    str += "Please enter a valid address";
    lines+=18;
  }*/

    
  if($.trim(phone) == "") {
    if(str!="") str+= "<br/>";
    str += "Please enter the Contact Phone";
    lines+=18;
  }
  if ($.trim(storehours) != "") {
    if(storehours.length >450) {
      if(str!="") str+= "<br/>";
      str += "Store hours should not be more than 450 characters";
      lines+=18;
    }
  }
  if(status == -1) {
    if(str!="") str+= "<br/>";
    str += "Please select any Status";
    lines+=18;
  }  
  if (str != "") {
    resize_dialog(680,300+lines);
    styleStatusMessages(str, "statusMessage", "failure");    
    $('#dialog').scrollTop(0);
    return false;
  }
  
	
  storeallvalues(-1);

  $('#dialog').html('<div style="padding-top:150px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  var confirm=0;
  $.ajax({
    type: "POST",
    url : baseurl+'findastore/save/store',
    data: '&storename='+ storename + '&category=' + category + '&address1=' + address1 + '&address2=' + address2 + '&zipcode=' + zipcode + '&state=' + state + '&city=' + city + '&country=' + country + '&status=' + status+ '&phone=' + phone+ '&phone2=' + phone2+"&phonetype="+phonetype + "&phonetypeadditional="+phonetype_additional+"&storehours="+storehours+"&usstates="+usstates+"&confirmFlag="+confirm+"&countryname="+countryname,
    success: function(res){
      eval("var json="+res);
      if(json.Message == 'Success')
      {
	  
        var pageno   = $('#hid_pageno').val();
        var module   = $('#module').val();
        if(catid > 0)
        {
						
          reopen_dialog(500,210,catname);
          categorydetailsDialoge(catid)
        }
		
		
        else
        {
          $("#dialog").dialog("destroy");
          openAlertDialog(300, 150, 'Information', 'Store Created Successfully','success');
				
          if(module == 'categories')
          {
            location.href = 'stores';
          }
					  
          else
          {
            applyStoresFilterAction(pageno,module);
          }
					  
        }
		
      }


      else
      {
        if(json.Error)
        {
          $.ajax({
            type : "GET",
            url : baseurl+'findastore/addstore',
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
              retainvalues();
              //if($.trim(country) == "United States") {
              if($.trim(country) == "US")
              {
                $('#usstates').show();
                $('#otherstates').hide();
                $("#edit-find-usstates option[value="+usstates+"]").attr("selected", "selected");
              }
              else
              {
                $('#otherstates').show();
                $('#usstates').hide();
		
              }

      

      
    
	
              var confirm="Confirm";
              openConfirmBox(function(){
                $.ajax({
                  type: "POST",
                  url : baseurl+'findastore/save/store',
                  data: '&storename='+ storename + '&category=' + category + '&address1=' + address1 + '&address2=' + address2 + '&zipcode=' + zipcode + '&state=' + state + '&city=' + city + '&country=' + country + '&status=' + status+ '&phone=' + phone+ '&phone2=' + phone2+"&phonetype="+phonetype + "&phonetypeadditional="+phonetype_additional+"&storehours="+storehours+"&usstates="+usstates+"&confirmFlag="+confirm,
                  success: function(res)
                  {
                    eval("var json="+res);
                    if(json.Message == 'Success')
                    {
                      var pageno   = $('#hid_pageno').val();
                      var module   = $('#module').val();
			
                      if(catid > 0)
                      {
                        reopen_dialog(500,210,catname);
                        categorydetailsDialoge(catid)
                      }
			
                      else
                      {
                        $("#dialog").dialog("destroy");
                        openAlertDialog(300, 150, 'Information', 'Store Created Successfully','success');
                        if(module == 'categories')
                        {
                          location.href = 'stores';
                        }
                        else
                        {
                          applyStoresFilterAction(pageno,module);
                        }
                      }
                    }
			
                    else
                    {
                      if(json.Error)
                      {
                        $.ajax({
                          type : "GET",
                          url : baseurl+'findastore/addstore',
                          cache : false,
                          success : function (res)
                          {
					
                            $('#dialog').html(res);
                            openAlertDialog(300, 150, 'Information', 'Store Created Successfully','success');
                            retainvalues();
                            if($.trim(country) == "US")
                            {
                              $('#usstates').show();
                              $('#otherstates').hide();
                              $("#edit-find-usstates option[value="+usstates+"]").attr("selected", "selected");
                            }
                            else
                            {
                              $('#otherstates').show();
                              $('#usstates').hide();
							
                            }
                          }
								
					  
                        });
			  
                      }
			
			
					
                    }
                  }
                });
                $('#alertdialog').dialog('destroy');
              }, 300, 120, "Invalid postalcode/address Confirm Save", "Do you want to save this store?");

                                                 
              $('#dialog').scrollTop(0);
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
          
          resize_dialog(680,470+50);
        }
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;

}


function addCategory(){
  openDialog("addcategory", 0, 600, 250,"Add Category");
}

function addCategorydialog(){
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/addcategory',
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#edit-category-name').focus();
      $('#edit-category-status').val('Active');
		  
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function saveCategory(storeid){
 
  var categoryname   =  $('#edit-category-name').val();
  var description    =  $('#edit-category-desc').val();
  var status         =  $('#edit-category-status').val();
    
  var lines = 10;
  var str = "";

  if(categoryname == '') {
    if(str!="")
      str+= "<br/>";
    str += "Please enter the Category name";
    lines+=18;
  }

  if(status == 'Any') {
    if(str!="")
      str+= "<br/>";
    str += "Please select the Status";
    lines+=18;
  }

  if (str != "") {
    resize_dialog(600, 250 + lines);
    styleStatusMessages(str, "statusMessage", "failure");
    $('#dialog').scrollTop(0);
    return;
  }
		
    
   
  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');

  $.ajax({
    type: "POST",
    url : baseurl+'findastore/save/category',
    cache : false,
    data: '&categoryname='+ encodeURIComponent(categoryname) + '&description=' + encodeURIComponent(description) + '&status=' + status,
    success: function(res){
      // alert(res);
      eval("var json="+res);
      if(json.Message == 'Success'){
		 

        if(storeid>0){
          resize_dialog(680, 420);
          $.ajax({
            type : "GET",
            url : baseurl+'findastore/edit/'+storeid,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
              catvalue = $("#edit-find-category option[text="+categoryname+"]").val();
              if(catvalue > 0){
                if(storesArray["categoryname"]){
                  storesArray["categoryname"].push(catvalue);
                }
                else{
                  storesArray["categoryname"] = catvalue;
                }
              }
                             
              retainvalues();
              //if($('#edit-find-country').val() == "United States")
              if($('#edit-find-country').val() == "US")
              {
                $('#usstates').show();
                $('#otherstates').hide();
                $("#edit-find-usstates").val(storesArray["usstate"]);

              }
              else
              {
                $('#usstates').hide();
                $('#otherstates').show();
              }

                                                          
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
        }
        else if(storeid == 0){

          reopen_dialog(680, 420,"Add Store");
          $.ajax({
            type : "GET",
            url : baseurl+'findastore/addstore/'+storeid,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
							  
              //$("#edit-find-category").val(categoryname);
              //alert(storesArray['newcategory']);
              catvalue = $("#edit-find-category option[text="+categoryname+"]").val();
              if(catvalue > 0){
                if(storesArray["categoryname"]){
                  storesArray["categoryname"].push(catvalue);
                }
                else{
                  storesArray["categoryname"] = catvalue;
                }
              }
                               	 
              retainvalues();
              // if($('#edit-find-country').val() == "United States")
              if($('#edit-find-country').val() == "US")
              {
                $('#usstates').show();
                $('#otherstates').hide();
                $("#edit-find-usstates").val(storesArray["usstate"]);

              }
              else
              {
                $('#usstates').hide();
                $('#otherstates').show();
              }
							    
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
        }

        else{
          var pageno   = $('#hid_pageno').val();
          var module   = $('#module').val();
					 
          if(module == 'stores'){
            //applyStoresFilterAction(pageno,module);
            location.href = 'categories';
          }
          else{
            applyCategoriesFilterAction(pageno,module);
          }
          reopen_dialog(300, 150,"Information");
          $('#dialog').dialog('destroy');
          openAlertDialog(300, 150, 'Information', 'Category Created Successfully','success');
        }
      }
      else{
        if(json.Error){
          $.ajax({
            type : "GET",
            url : baseurl+'findastore/addcategory/'+storeid,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
              $("#edit-category-name").val(categoryname);
              $("#edit-category-status").val(status);
              $("#edit-category-desc").val(description);
              styleStatusMessages(json.Error, "statusMessage", "failure");
              $('#dialog').scrollTop(0);
            },
            error : function (res) {
              handleRequestError(res);
            }
          });

          resize_dialog(600, 250 + lines);

          return;
        }

        reopen_dialog(300, 225,"Error");
        $('#dialog').dialog('destroy');
        openAlertDialog(300, 150, 'Error', 'Failed saving the data');
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;

}


function assignStore(){
  categoryname = $('#category_name').html();
  reopen_dialog(500,250,"Assign Store");
  assignStoreDialoge(categoryname);
}

function assignStoreDialoge(categoryname){	

  $.ajax({
    type : "GET",
    url : baseurl+'findastore/assignstore',
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#category_name').val(categoryname);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}


function assignValues(){
  var src = document.getElementById('available_store');
  var dest = document.getElementById('assigned_store');
  for(var count=0; count < src.options.length; count++) {
    if(src.options[count].selected == true) {
      var option = src.options[count];
      var newOption = document.createElement("option");
      newOption.value = option.value;
      newOption.text = option.text;
      newOption.selected = true;
      try {
        dest.add(newOption, null); //Standard
        src.remove(count, null);
      }catch(error) {
        dest.add(newOption); // IE only
        src.remove(count);
      }
      count--;
    }
  }
}


function reAssignValues(){
  var src = document.getElementById('assigned_store');
  var dest = document.getElementById('available_store');
  for(var count=0; count < src.options.length; count++) {
    if(src.options[count].selected == true) {
      var option = src.options[count];
      var newOption = document.createElement("option");
      newOption.value = option.value;
      newOption.text = option.text;
      newOption.selected = true;
      try {
        dest.add(newOption, null); //Standard
        src.remove(count, null);
      }catch(error) {
        dest.add(newOption); // IE only
        src.remove(count);
      }
      count--;
    }
  }
}


function assignStoresave(){
  categoryname  =  $('#category_name').val();
  var src = document.getElementById('assigned_store');
  var strstores='';
  for(var count=0; count < src.options.length; count++) {
    var option = src.options[count];
    var newOption = document.createElement("option");
    newOption.value = option.value;
    newOption.text = option.text;
    newOption.selected = true;
    strstores = strstores +','+newOption.value;
  }
  strstores = strstores.substring(1);


  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type: "POST",
    url : baseurl+'findastore/assignstoresave',
    cache : false,
    data: '&categoryname='+ categoryname + '&storelist=' + strstores,
    success: function(res){
      if(res == 'Success'){
        reopen_dialog(300,225,"Information");
        $('#dialog').dialog('destroy');
        openAlertDialog(300, 150, 'Information', 'Updated Successfully','success');
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}


function cancelAddStore(id,catname){
  reopen_dialog(500,210,catname);
  categorydetailsDialoge(id)
}
function categorydetailsDialoge(id){
  $('#dialog').html('<div style="padding-top:50px; padding-left:220px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/viewcategorydetails/'+id,
    cache : false,
    success : function (res) {
      //alert(res);
      $('#dialog').html(res);

    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}


function storedetailsDialoge(id){
  $('#dialog').html('<div style="padding-top:150px; padding-left:220px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/viewstoredetails/'+id,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

                     

						  

function categoryActivation(id){
  var category_name         =  $('#category_name').html();
  var category_description  =  $('#category_description').html();
  var category_status       =  $('#category_status').html();

  var pageno   = $('#hid_pageno').val();
  var module   = $('#module').val();
  //alert(category_status);
  var activestore_count     = $('#edit-active-store').val();
  var inactivestore_count   = $('#edit-inactive-store').val();
   
  var total_count = parseInt(activestore_count) +parseInt(inactivestore_count);

  if(activestore_count > 0) {
    var message = "Store(s) are associated with this category.Do you want to deactivate?";
  }
  else if(total_count == 0){
    var message = "No stores in the category. Do you want to Deactive?";
  }
  else if(total_count ==  inactivestore_count){
    var message = "All the stores are Inactive. Do you want to Deactive?";
  }



  if(category_status == "Active"){
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#dialog').html('<div style="padding-top:50px; padding-left:220px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
			
      $.ajax({
        type: "POST",
        url : baseurl+'findastore/categoryactivation/Deactivate/',
        cache : false,
        data: '&catvalues='+ id,
        success: function(res){
          eval("var json="+res);

          if(json.Message == 'Success'){
            applyCategoriesFilterAction(pageno,module);
          
            
            //$('#dialog').dialog('destroy');
            
            //  categorydetailsDialoge(id);
            reopen_dialog(500,210,category_name);
            openDialog('categoriesdetails',id, 500, 210, category_name);
            //reopen_dialog(300,150,"Information");
            openAlertDialog(300, 150, 'Information', 'Category deactivated successfully','success');
          }
          else{
            reopen_dialog(300,225,"Error");
            $('#dialog').dialog('destroy');
            openAlertDialog(300, 150, 'Error', 'Error in Deactivating');
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },400,100,"Confirm Deactivate", message);
    return false;
  }
  else{
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#dialog').html('<div style="padding-top:50px; padding-left:200px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
      $.ajax({
        type: "POST",
        url : baseurl+'findastore/categoryactivation/Activate/',
        cache : false,
        data: '&catvalues='+ id ,
        success: function(res){
          eval("var json="+res);
				
          if(json.Message == 'Success'){
            applyCategoriesFilterAction(pageno,module);
            //categorydetailsDialoge(id);
            reopen_dialog(500,210,category_name);
            //$('#dialog').dialog('destroy');
             openDialog('categoriesdetails',id, 500, 210, category_name);
            openAlertDialog(300, 150, 'Information', 'Category activated successfully','success');
          }
          else{
            reopen_dialog(300,225,"Error");
            $('#dialog').dialog('destroy');
            openAlertDialog(300, 150, 'Error', 'Error in Activating');
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },400,100,"Confirm Activate", "Do you want to activate this category?");
  }

  return false;
}


function storeActivation(id){	
  var store_status       =  $('#store_status').html();
  var pageno             =  $('#hid_pageno').val();
  var module             =  $('#module').val();
  var storename          =  $('#store_name').html();
				  
  if(store_status == "Active"){
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#dialog').html('<div style="padding-top:150px; padding-left:240px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
      $.ajax({
        type: "POST",
        url : baseurl+'findastore/storeactivation/Deactivate/',
        data: '&storevalues='+ id,
        cache : false,
        success: function(res){
          eval("var json="+res);
          if(json.Message == 'Success'){
            applyStoresFilterAction(pageno,module);
            //storedetailsDialoge(id);
            reopen_dialog(500,410,storename);
            openDialog('storesdetails',id, 500, 410, storename);
            //$('#dialog').dialog('destroy');
            openAlertDialog(300, 150, 'Information', 'Store deactivated successfully','success');
          }
          else{
            reopen_dialog(300,225,"Error");
            $('#dialog').dialog('destroy');
            openAlertDialog(300,200,"Error", "Deactivation not done");
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },400,100,"Confirm Deactivate", "Do you want to deactivate this store?");
  }
  else{
    ConfirmBox(function(){
      $('#alertdialog').dialog('destroy');
      $('#dialog').html('<div style="padding-top:150px; padding-left:240px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
      $.ajax({
        type: "POST",
        url : baseurl+'findastore/storeactivation/Activate/',
        data: '&storevalues='+ id,
        cache : false,
        success: function(res){
          eval("var json="+res);
          //alert(res);
          if(json.Message == 'Success'){
            applyStoresFilterAction(pageno,module);
            //storedetailsDialoge(id);
            reopen_dialog(500,410,storename);
            openDialog('storesdetails',id, 500, 410, storename);
            //$('#dialog').dialog('destroy');
            openAlertDialog(300, 150, 'Information', 'Store activated successfully','success');
          }
          else{
            reopen_dialog(300,225,"Error");
            $('#dialog').dialog('destroy');
            openAlertDialog(300,200,"Error", "Activation not done");
          }
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    },400,100,"Confirm Activate", "Do you want to activate this store?");
  }
  return false;
}



function editStore(id){

  var category_name = $('#category_name').html();
  storeallvalues();
  $('#dialog').html('<div style="padding-top:150px; padding-left:280px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
	
  resize_dialog(680, 420);
  	
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/edit/'+id,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#edit-find-store-name').focus();
     
      //if($('#edit-find-country').val() == "United States")
      if($('#edit-find-country').val() == "US")
      {
        $('#usstates').show();
        $('#otherstates').hide();
      }
      else
      {
        $('#usstates').hide();
        $('#otherstates').show();
      }

    },
    error : function (res) {
      handleRequestError(res);
    }
  });
  return false;
}

function updateCategory(id){
  var categoryname =  $('#edit-category-name').val();
  var desc         =  $('#edit-category-desc').val();
  var status       =  $('#edit-category-status').val();

  var lines = 10;
  var str = "";

  if(categoryname == '') {
    if(str!="")
      str+= "<br/>";
    str += "Please enter the Category name";
    lines+=18;
  }

  if(status == 'Any') {
    if(str!="")
      str+= "<br/>";
    str += "Please select the Status";
    lines+=18;
  }

  if (str != "") {
    resize_dialog(550, 250 + lines);
    styleStatusMessages(str, "statusMessage", "failure");
    $('#dialog').scrollTop(0);
    return;
  }


  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
    
  $.ajax({
    type: "POST",
    url : baseurl+'findastore/update/category/'+id,
    cache : false,
    data: '&categoryname='+ encodeURIComponent(categoryname) + '&desc=' + encodeURIComponent(desc) + '&status=' + status ,
    success: function(res){
      //alert(res);

      eval("var json="+res);
				
      if(json.Message == 'Success'){
        var pageno   = $('#hid_pageno').val();
        var module   = $('#module').val();
        applyCategoriesFilterAction(pageno,module);
        reopen_dialog(300,150,"Information");
        $('#dialog').dialog('destroy');
        openAlertDialog(300, 150, 'Information', 'Category Updated Successfully','success');
      }
      else{
        if(json.Error){
          resize_dialog(550,270);
          $.ajax({
            type : "GET",
            url : baseurl+'findastore/edit/category/'+id,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
              $('#edit-category-name').val(categoryname);
              $('#edit-category-desc').val(desc);
              $('#edit-category-status').val(status);
              styleStatusMessages(json.Error, "statusMessage", "failure");
              $('#dialog').scrollTop(0);
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
        }
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
   
  return false;
}

function updateStore(storeid,workflowId){
  $("#alertdialog").dialog("destroy");
  var store_name        =  $('#edit-find-store-name').val();
  var category_name     =  $('#edit-find-category').val();
  var store_address1    =  $('#edit-find-address1').val();
  var store_address2    =  $('#edit-find-address2').val();
  var store_zipcode     =  $('#edit-find-zipcode').val();

  var store_state       =  $('#edit-find-state').val();
  var store_usstate       =  $('#edit-find-usstates').val();


  var store_city        =  $('#edit-find-city').val();
  var store_country     =  $('#edit-find-country').val();
  var phone             =  $('#edit-work-phone').val();
  var phone2            =  $('#edit-work-phone-additional').val();
  var store_status      =  $('#edit-find-status').val();   
  var phonetype   =  $('#edit-work-phone-type').val();
  var phonetype_additional   =  $('#edit-work-phone-additional-type').val();
  var storehours    = $('#edit-storehours').val();


  var pageno   = $('#hid_pageno').val();
  var module   = $('#module').val();

  var selected = $("#edit-find-country option:selected");
  if(selected.val() != -1)
  {
    var countryname=selected.text();
  }


  //code to validate the fields
  var lines = 10;
  var str = "";
  if ($.trim(store_name) == "") {
    str += "Please enter the Store Name";
    lines+=18;
  }
  if ($.trim(store_name) != "") {
    if(store_name.length > 50) {
      if(str!="")
        str+= "<br/>";
      str += "Store name should not be more than 50 characters";
      lines+=18;
    }
  }

  if((category_name == null) || (category_name == '')){
    if(str!="")
      str+= "<br/>";
    str += "Please select atleast one Category";
    lines+=18;
  }

  if(store_country == "") {
    if(str!="")
      str+= "<br/>";
    str += "Please select any Country";
    lines+=18;
  }


  if($.trim(store_address1) == "") {
    if(str!="")
      str+= "<br/>";
    str += "Please enter the Address Line1";
    lines+=18;
  }

  if($.trim(store_city) == "") {
    if(str!="")
      str+= "<br/>";
    str += "Please enter the City";
    lines+=18;
  }

  // if(store_country == "United States") {
  if(store_country == "US") {
    var store_state = $('#edit-find-usstates').val();
    if(store_state == "") {
      if(str!="")
        str+= "<br/>";
      str += "Please select the State / Province";
      lines+=18;
    }
  }
  else {
    var store_state =  $('#edit-find-state').val();
    if(store_state == "") {
      if(str!="")
        str+= "<br/>";
      str += "Please enter the State / Province";
      lines+=18;
    }
  }
  if($.trim(store_zipcode) == "") {
    if(str!="")
      str+= "<br/>";
    str += "Please enter the Postal Code";
    lines+=18;
  }
  if($.trim(phone) == "") {
    if(str!="") str+= "<br/>";
    str += "Please enter the Contact Phone";
    lines+=18;
  }

  if ($.trim(storehours) != "") {
    if(storehours.length >450)
    {
      if(str!="") str+= "<br/>";
      str += "Store hours should not be more than 450 characters";
      lines+=18;

    }
  }

  if(store_status == -1) {
    if(str!="")
      str+= "<br/>";
    str += "Please select any Status";
    lines+=18;
  }

  if (str != "") {
    resize_dialog(680,300+lines);
    styleStatusMessages(str, "statusMessage", "failure");
    $('#dialog').scrollTop(0);
    return;
  }
  var confirmationmsg="createStore";

  storeallvalues(-1);
   


  $('#dialog').html('<div style="padding-top:170px; padding-left:300px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
 
  
  $.ajax({
    type: "POST",
    url : baseurl+'findastore/update/score/'+storeid+'/'+workflowId,
    cache : false,
    data: 'store_name='+ store_name + '&category_name=' + category_name + '&store_address1=' + store_address1 + '&store_address2=' + store_address2 + '&store_zipcode=' + store_zipcode + '&store_state=' + store_state + '&store_city=' + store_city + '&store_country=' + store_country +  '&store_status=' + store_status+ '&phone=' + phone+ '&phone2=' + phone2 +"&phonetype="+phonetype + "&phonetypeadditional="+phonetype_additional+"&storehours="+storehours+"&confirmFlag="+confirmationmsg+"&store_countryname="+countryname,

    success: function(res){
      
      eval("var json="+res);
      if(json.Message == 'Success'){
        $('#dialog').dialog('destroy');
        openAlertDialog(310, 160, 'Information', 'Store Updated Successfully','success');
        applyStoresFilterAction(pageno,'store');
        
      }
			
      else {
        if(json.Error){
          $.ajax({
            type : "GET",
            url : baseurl+'findastore/edit/'+storeid,
            cache : false,
            success : function (res) {
              $('#dialog').html(res);
                                                         
                                                         
              retainvalues();

              //if($.trim(store_country) == "United States") {
              if($.trim(store_country) == "US") {
                $('#usstates').show();
                $('#otherstates').hide();
                $("#edit-find-usstates option[value="+store_state+"]").attr("selected", "selected");
              }
              else
              {
                $('#otherstates').show();
                $('#usstates').hide();

              }


              var confirmationmsgyes="Confirm";
	
	
   
	
              openConfirmBox(function(){
                var postedData = 'store_name='+ store_name + '&category_name=' + category_name + '&store_address1=' + store_address1 + '&store_address2=' + store_address2 + '&store_zipcode=' + store_zipcode + '&store_state=' + store_state + '&store_city=' + store_city + '&store_country=' + store_country +  '&store_status=' + store_status+ '&phone=' + phone+ '&phone2=' + phone2 +"&phonetype="+phonetype + "&phonetypeadditional="+phonetype_additional+"&storehours="+storehours+"&confirmFlag="+confirmationmsgyes;
						
                $.ajax({
                  type: "POST",
                  url : baseurl+'findastore/update/score/'+storeid+'/'+workflowId,
                  cache : false,
                  data: postedData,
                  success: function(res){
                    eval("var json1="+res);
                    if(json1.Message == 'Success'){
                      $('#dialog').dialog('destroy');
                      applyStoresFilterAction(pageno,'store');
                      openAlertDialog(300, 150, 'Information', 'Store Updated Successfully','success');
                    }
			
                    else
                    {
                      if(json.Error){
                        $.ajax({
                          type : "GET",
                          url : baseurl+'findastore/edit/'+storeid,
                          cache : false,
                          success : function (res) {
                            $('#dialog').html(res);
                                                         
                                                         
                            //retainvalues();

                            //if($.trim(store_country) == "United States") {
                            if($.trim(store_country) == "US")
                            {
                              $('#usstates').show();
                              $('#otherstates').hide();
                              $("#edit-find-usstates option[value="+store_state+"]").attr("selected", "selected");
                            }
                            else
                            {
                              $('#otherstates').show();
                              $('#usstates').hide();

                            }

 

        
                          }
                        });
                      }
                      }
                    }
  

                });
                $('#alertdialog').dialog('destroy');
              }, 300, 120, "Confirm Update", "Invalid postalcode/address Do you want to update this store?");
              $('#dialog').scrollTop(0);
            },
            error : function (res) {
              handleRequestError(res);
            }
          });
          resize_dialog(680, 460 + 50);
        }
      }
			  
    },
    error : function (res) {
      handleRequestError(res);
    }
  });

}

function cancelDialoge(){
  $("#dialog").dialog("destroy");
  return false;
}

function cancelEdit(catid){
  var catname = $('#edit-category-name').val();
  reopen_dialog(500,210,catname);
  categorydetailsDialoge(catid);
}

function cancelEditstore(storeid){
  //var storename = $('#edit-find-store-name').val();
  var storename = storesArray["storename"]
  reopen_dialog(500,420,storename);
  storedetailsDialoge(storeid);
}


function cancelDialogeCategory(storeid,categoryid){
  $('#dialog').html('<div style="padding-top:90px; padding-left:210px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
     
  if(storeid>0){
    resize_dialog(680, 420);
    $.ajax({
      type : "GET",
      url : baseurl+'findastore/edit/'+storeid,
      cache : false,
      success : function (res) {
        $('#dialog').html(res);
        retainvalues();
        //if($('#edit-find-country').val() == "United States")
        if($('#edit-find-country').val() == "US")
        {
          $('#usstates').show();
          $('#otherstates').hide();
          $("#edit-find-usstates").val(storesArray["usstate"]);
        }
        else
        {
          $('#usstates').hide();
          $('#otherstates').show();
        }
        $('#edit-find-store-name').focus();

      },
      error : function (res) {
        handleRequestError(res);
      }
    });
  }
  else if(storeid == 0){
    resize_dialog(680, 420);
    if(categoryid == 0){
      reopen_dialog(680, 420,"Add Store");
      $.ajax({
        type : "GET",
        url : baseurl+'findastore/addstore',
        cache : false,
        success : function (res) {
          $('#dialog').html(res);
          if(storesArray["categoryname"] == '') storesArray["categoryname"] = -1;
          retainvalues();
          //if($('#edit-find-country').val() == "United States")
          if($('#edit-find-country').val() == "US")
          {
            $('#usstates').show();
            $('#otherstates').hide();
            $("#edit-find-usstates").val(storesArray["usstate"]);

          }
          else
          {
            $('#usstates').hide();
            $('#otherstates').show();
          }


          $('#edit-find-store-name').focus();
								  
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    }
    else{
      $.ajax({
        type : "GET",
        url : baseurl+'findastore/addstore/'+storesArray["category_name"]+'/'+categoryid,
        cache : false,
        success : function (res) {
          $('#dialog').html(res);
          $("#edit-find-store-name").focus();
          $('#edit-find-category').val(categoryid);
        /*
							  $('#set_timings_mon').hide();
							  $('#set_timings_tue').hide();
							  $('#set_timings_wed').hide();
							  $('#set_timings_thu').hide();
							  $('#set_timings_fri').hide();
							  $('#set_timings_sat').hide();
							  $('#set_timings_sun').hide();
							  */
        },
        error : function (res) {
          handleRequestError(res);
        }
      });
    }
  }
  else{
    $("#dialog").dialog("destroy");
  }
  return false;

}

function retainvalues(){
  $('#edit-find-store-name').val(storesArray["storename"]);
  
  $('#edit-find-category').val(storesArray["categoryname"]);
  $('#edit-find-address1').val(storesArray["address1"]);
  $('#edit-find-address2').val(storesArray["address2"]);
  $('#edit-find-zipcode').val(storesArray["postalcode"]);
  $('#edit-find-country').val(storesArray["country"]);

  //if($('#edit-find-country').val(storesArray["country"]) == "United States")
  if($('#edit-find-country').val(storesArray["country"]) == "US")
    $('#edit-find-usstates').val(storesArray["usstate"]);
  else
    $('#edit-find-state').val(storesArray["state"]);


  $('#edit-find-city').val(storesArray["city"]);
		
  //if($('#edit-find-country').val(storesArray["country"]) == "United States")
  if($('#edit-find-country').val(storesArray["country"]) == "US")
  {
    $('#usstates').show();
    $('#otherstates').hide();
    $("#edit-find-usstates option[value="+$('#edit-find-usstates').val(storesArray['state'])+"]").attr("selected", "selected");
  }
  else
  {
    $('#usstates').hide();
    $('#otherstates').show();
  }
               
                   
  $('#edit-work-phone').val(storesArray["contactphone"]);
  //$('#edit-work-phone-additional').val(storesArray["addphone"]);

  //$('#edit-24-hrs').val(storesArray["24hrs"]);
  $('#edit-find-status').val(storesArray["status"]);

  $('#edit-work-phone-additional').val(storesArray["addphone"]);
  $('#edit-work-phone-type').val(storesArray["contactphone_type"]);
  $('#edit-work-phone-additional-type').val(storesArray["addphone_type"]);

  $('#edit-opening-time-hr-mon').val(storesArray["openingtime_hr_mon"]);
  $('#edit-opening-time-min-mon').val(storesArray["openingtime_min_mon"]);
  $('#edit-closing-time-hr-mon').val(storesArray["closingtime_hr_mon"]);
  $('#edit-closing-time-min-mon').val(storesArray["closingtime_min_mon"]);
  $('#edit-opening-time-mon').val(storesArray["openingtime_type_mon"]);
  $('#edit-closing-time-sun').val(storesArray["closingtime_type_mon"]);


  $('#edit-opening-time-hr-tue').val(storesArray["openingtime_hr_tue"]);
  $('#edit-opening-time-min-tue').val(storesArray["openingtime_min_tue"]);
  $('#edit-closing-time-hr-tue').val(storesArray["closingtime_hr_tue"]);
  $('#edit-closing-time-min-tue').val(storesArray["closingtime_min_tue"]);
  $('#edit-opening-time-tue').val(storesArray["openingtime_type_tue"]);
  $('#edit-closing-time-tue').val(storesArray["closingtime_type_tue"]);

  $('#edit-opening-time-hr-wen').val(storesArray["openingtime_hr_wen"]);
  $('#edit-opening-time-min-wen').val(storesArray["openingtime_min_wen"]);
  $('#edit-closing-time-hr-wen').val(storesArray["closingtime_hr_wen"]);
  $('#edit-closing-time-min-wen').val(storesArray["closingtime_min_wen"]);
  $('#edit-opening-time-wen').val(storesArray["openingtime_type_wen"]);
  $('#edit-closing-time-wen').val(storesArray["closingtime_type_wen"]);

  $('#edit-opening-time-hr-thu').val(storesArray["openingtime_hr_thu"]);
  $('#edit-opening-time-min-thu').val(storesArray["openingtime_min_thu"]);
  $('#edit-closing-time-hr-thu').val(storesArray["closingtime_hr_thu"]);
  $('#edit-closing-time-min-thu').val(storesArray["closingtime_min_thu"]);
  $('#edit-opening-time-thu').val(storesArray["openingtime_type_thu"]);
  $('#edit-closing-time-thu').val(storesArray["closingtime_type_thu"]);
		
  $('#edit-opening-time-hr-fri').val(storesArray["openingtime_hr_fri"]);
  $('#edit-opening-time-min-fri').val(storesArray["openingtime_min_fri"]);
  $('#edit-closing-time-hr-fri').val(storesArray["closingtime_hr_fri"]);
  $('#edit-closing-time-min-fri').val(storesArray["closingtime_min_fri"]);
  $('#edit-opening-time-fri').val(storesArray["openingtime_type_fri"]);
  $('#edit-closing-time-fri').val(storesArray["closingtime_type_fri"]);

  $('#edit-opening-time-hr-sat').val(storesArray["openingtime_hr_sat"]);
  $('#edit-opening-time-min-sat').val(storesArray["openingtime_min_sat"]);
  $('#edit-closing-time-hr-sat').val(storesArray["closingtime_hr_sat"]);
  $('#edit-closing-time-min-sat').val(storesArray["closingtime_min_sat"]);
  $('#edit-opening-time-sat').val(storesArray["openingtime_type_sat"]);
  $('#edit-closing-time-sat').val(storesArray["closingtime_type_sat"]);

  $('#edit-opening-time-hr-sun').val(storesArray["openingtime_hr_sun"]);
  $('#edit-opening-time-min-sun').val(storesArray["openingtime_min_sun"]);
  $('#edit-closing-time-hr-sun').val(storesArray["closingtime_hr_sun"]);
  $('#edit-closing-time-min-sun').val(storesArray["closingtime_min_sun"]);
  $('#edit-opening-time-sun').val(storesArray["openingtime_type_sun"]);
  $('#edit-closing-time-sun').val(storesArray["closingtime_type_sun"]);
  //$('#edit-timezone').val(storesArray["timezone"]);
  $('#edit-storehours').val(storesArray["storehours"]);


		
  if(storesArray["week_mon_check"]){
    $("#edit-week-mon").attr('checked', storesArray["week_mon_check"]);
    $('#set_timings_mon').show();
  }
  else{
    $('#set_timings_mon').hide();
  }
  if(storesArray["week_tue_check"]){
    $("#edit-week-tue").attr('checked', storesArray["week_tue_check"]);
    $('#set_timings_tue').show();
  }
  else{
    $('#set_timings_tue').hide();
  }
  if(storesArray["week_wed_check"]){
    $("#edit-week-wed").attr('checked', storesArray["week_wed_check"]);
    $('#set_timings_wed').show();
  }
  else{
    $('#set_timings_wed').hide();
  }
  if(storesArray["week_thu_check"]){
    $("#edit-week-thu").attr('checked', storesArray["week_thu_check"]);
    $('#set_timings_thu').show();
  }
  else{
    $('#set_timings_thu').hide();
  }
  if(storesArray["week_fri_check"]){
    $("#edit-week-fri").attr('checked', storesArray["week_fri_check"]);
    $('#set_timings_fri').show();
  }
  else{
    $('#set_timings_fri').hide();
  }
  if(storesArray["week_sat_check"]){
    $("#edit-week-sat").attr('checked', storesArray["week_sat_check"]);
    $('#set_timings_sat').show();
  }
  else{
    $('#set_timings_sat').hide();
  }
  if(storesArray["week_sun_check"]){
    $("#edit-week-sun").attr('checked', storesArray["week_sun_check"]);
    $('#set_timings_sun').show();
  }
  else{
    $('#set_timings_sun').hide();
  }
       
		
		
}

function view_store(categoryid){
  var category = $('#category_name').html();
  var pageno   = $('#hid_pageno').val();
  var module   = $('#module').val();

  document.cookie = "category="+category+"; path=/";

  //$('#dialog').html('<div style="padding-top:50px;padding-bottom:50px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $('#dialog').dialog('destroy');

  location.href = 'stores/viewstore/'+categoryid

  // categoryapplyAction(pageno,module,categoryid);
  return false;
}


function categoryapplyAction(pageno, action,categoryid){
  var sort = $('#hid_sort').val();

  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").hide();
  $("#pagingdivbottom").hide();

  $.ajax({
    type : "POST",
    url : baseurl+"findastore/actions/filter/category",
    data : "category="+categoryid+(pageno>1?'&page_no='+pageno:'')+(sort!=""?'&sorting='+sort:''),
    cache : false,
    success : function (msg) {
      //alert(msg);
      $('#dialog').dialog('destroy');
      $("#listingResultset").html(msg);
			 
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno,10);
      var counter = $("#hid_totalRec").val();
	  
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "categoryapplyAction('+(page-1)+',\''+action+'\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += '<span class="red"> |</span><a href="javascript:void(0)" onclick = "categoryapplyAction('+(page+1)+',\''+action+'\')" class="red"> Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
        $("#pagingdivtop").show();
        $("#pagingdivbottom").show();
      }
		
    }
  });

}



function FilterAction(){
  //var pageno   = $('#hid_pageno').val();
  var pageno =1;
  var module   = $('#module').val();
  $('#checkAll').attr('checked', false);
  if(module == 'categories'){
    applyCategoriesFilterAction(pageno,module);
  }
  else{
    applyStoresFilterAction(pageno,module);
  }
  return false;
}


function applyCategoriesFilterAction(pageno,action){
  //prepare the field values for filtering
  var search_catname      =  $.trim($("#edit-ct-search-categoryname").val());
  
  var search_status       =  $("#edit-ct-search-status").val();
  var search_date         =  $("#edit-ct-search-date").val();
  var sort                =  $('#hid_sort').val();
  var module              =  $('#module').val();

  $('#hid_pageno').val(pageno);

  //check for search criteria and return false if no value
  if (search_catname == "" && search_status == "" && search_date == "" ) {
    $('#dialog').dialog('destroy');
    openAlertDialog(300, 150, 'Information', 'Please enter the search criteria','information');
    return;
  }

  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").hide();
  $("#pagingdivbottom").hide();
  if($('input[name=checkAll]').is(':checked')){
    $('input[name=checkAll]').attr('checked', false);
  }

  $.ajax({
    type : "POST",
    url : baseurl+"findastore/actions/filter/"+module+'search',
    data : "search_catname="+encodeURIComponent(search_catname)+"&search_status="+search_status+"&search_date="+search_date+(pageno>1?'&page_no='+pageno:'')+(sort!=""?'&sorting='+sort:''),
    cache : false,
    success : function (msg) {
 
      $("#listingResultset").html(msg);
			 
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno,10);
      var counter = $("#hid_totalRec").val();
	
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyCategoriesFilterAction('+(page-1)+',\''+action+'\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += '<span class="red"> |</span><a href="javascript:void(0)" onclick = "applyCategoriesFilterAction('+(page+1)+',\''+action+'\')" class="red"> Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
        $("#pagingdivtop").show();
        $("#pagingdivbottom").show();
      }
		
    }
  });
  return false;
}




function applyStoresFilterAction(pageno,action){
  //prepare the field values for filtering
  var search_storename    =  $.trim($("#edit-st-search-storename").val());
  var search_category_id      = $("#edit-st-search-category").val();
  var search_status       =  $("#edit-st-search-status").val();
  var search_date         =  $("#edit-st-search-date").val();
  $('#hid_pageno').val(pageno);

  var search_categoryname = $("#edit-st-search-category option:selected").text();
  
 
  var sort                =  $('#hid_sort').val();
  var module              =  $('#module').val();
  if(action == 'list') module = action;

  //check for search criteria and return false if no value
  if (search_storename == "" && search_category_id == "" && search_status == "" && search_date == "") {
    $('#dialog').dialog('destroy');
    openAlertDialog(300, 150, 'Information', 'Please enter the search criteria','information');
    return;
  }

  $('#listingResultset').html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").hide();
  $("#pagingdivbottom").hide();
  if($('input[name=checkAll]').is(':checked')){
    $('input[name=checkAll]').attr('checked', false);
  }

  $.ajax({
    type : "POST",
    url : baseurl+"findastore/actions/filter/"+module+'search',
    data : "search_storename="+encodeURIComponent(search_storename)+"&search_category_id="+search_category_id+"&search_categoryname=" + search_categoryname +"&search_status="+search_status+"&search_date="+search_date+(pageno>1?'&page_no='+pageno:'')+(sort!=""?'&sorting='+sort:''),
    cache : false,
    success : function (msg) {

      $("#listingResultset").html(msg);
      var limit = $('#hid_limit').val();
      var page = parseInt(pageno,10);
      var counter = $("#hid_totalRec").val();
	
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyStoresFilterAction('+(page-1)+',\''+action+'\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += '<span class="red"> |</span><a href="javascript:void(0)" onclick = "applyStoresFilterAction('+(page+1)+',\''+action+'\')" class="red"> Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
        $("#pagingdivtop").show();
        $("#pagingdivbottom").show();
      }
		
    }
  });
  return false;
}


function edit_categories(id){
  $('#dialog').html('<div style="padding-top:50px; padding-left:260px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading... Please Wait !!!" title="Loading... Please Wait !!!" border="0"></div>');
  resize_dialog(550,250);
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/edit/category/'+id,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#edit-category-name').focus();
    },
    error : function (res) {
      handleRequestError(res);
    }
  });

}




function filterdata_categories(){
  //prepare the field values for filtering
  var search_cat         = $.trim($("#edit-ct-search-categoryname").val());
  var search_status      = $("#edit-ct-search-status").val();
  var search_date        = $("#edit-ct-search-date").val();

    
  var data = "";
  if($.trim(search_cat)!=""){
    data+="&search_catname="+search_cat;
  }

  if($.trim(search_status)!="" && search_status != "Any"){
    data+="&search_status="+search_status;
  }
  if($.trim(search_date)!=""){
    data+="&search_date="+search_date;
  }
  return data;
}

function filterdata_stores(){
  //prepare the field values for filtering
  var search_storename         = $.trim($("#edit-st-search-storename").val());
  var search_category          = $("#edit-st-search-category").val();
  var search_status            = $("#edit-st-search-status").val();
  var search_date              = $("#edit-st-search-date").val();

    
  var data = "";
  if($.trim(search_storename)!=""){
    data+="&search_storename="+search_storename;
  }

  if($.trim(search_category)!="" && (search_category != "ANY")){
    data+="&search_category_id="+search_category;
  }

  if($.trim(search_status)!="" && (search_status != "ANY")){
    data+="&search_status="+search_status;
  }

  if($.trim(search_date)!=""){
    data+="&search_date="+search_date;
  }

  return data;
}



function ConfirmBox (callback, width, height, title, content) {
  var wid = width;
  var heit = height;
  $('#alertdialog').dialog({
    autoOpen: false,
    width: wid,
    height: heit+20,
    bgiframe: true,
    resizable: false,
    title:title,
    modal: true,
    show: 'puff',
    buttons: {
      'Yes': callback,
      'No': function() {
        $('#alertdialog').dialog('destroy');
      }
    }
  })
  $('#alertdialog').show();
  // $('#alertdialog').html(content);
  //$('#alertdialog').dialog('open');


  var img_name = "warning.jpg"
  if((title == 'Confirm Deactivate') || (title == 'Confirm Activate') ){
    img_name = 'lg_triangle_yellow_questionmark.gif';
  }
  else if(title == 'Confirm Cancel Import'){
    img_name = 'lg_triangle_yellow_questionmark.gif';
  }


  $('#alertdialog').html('<div style="padding-left: 0px;"><span style="float: left;"><img border="0" src="'+baseurl+themeurl+'/images/'+img_name+'"/></span><span style="float: left; padding-left: 10px; padding-top: 5px;">'+content+'</span></div>');
  $('#alertdialog').dialog('open');
}


var storesArray= new Array()


function storeallvalues(storeid,categoryid){

  storesArray["storename"]      = $('#edit-find-store-name').val();
  storesArray["categoryname"]   = $('#edit-find-category').val();
  storesArray["address1"]       = $('#edit-find-address1').val();
  storesArray["address2"]       = $('#edit-find-address2').val();
  storesArray["postalcode"]     = $('#edit-find-zipcode').val();
  storesArray["state"]          = $('#edit-find-state').val();
  storesArray["usstate"]        = $('#edit-find-usstates').val();
   
  storesArray["city"]           = $('#edit-find-city').val();

  storesArray["country"]        = $('#edit-find-country').val();
 
  



  storesArray["contactphone"]   = $('#edit-work-phone').val();

  storesArray["addphone"]       = $('#edit-work-phone-additional').val();
  storesArray["contactphone_type"]   = $('#edit-work-phone-type').val();
  storesArray["addphone_type"]       = $('#edit-work-phone-additional-type').val();

  if($.trim(storesArray["storename"]) ==""){
    storesArray["storename"] = $('#store_name').html();
  }
  if(storesArray["categoryname"] == -1) storesArray["categoryname"] = "";
	
  /*
   storesArray["openingtime_hr_mon"]        = $('#edit-opening-time-hr-mon').val();
   storesArray["openingtime_min_mon"]       = $('#edit-opening-time-min-mon').val();
   storesArray["closingtime_hr_mon"]        = $('#edit-closing-time-hr-mon').val();
   storesArray["closingtime_min_mon"]       = $('#edit-closing-time-min-mon').val();
   storesArray["openingtime_type_mon"]          = $('#edit-opening-time-mon').val();
   storesArray["closingtime_type_mon"]          = $('#edit-closing-time-mon').val();
   

   storesArray["openingtime_hr_tue"]        = $('#edit-opening-time-hr-tue').val();
   storesArray["openingtime_min_tue"]       = $('#edit-opening-time-min-tue').val();
   storesArray["closingtime_hr_tue"]        = $('#edit-closing-time-hr-tue').val();
   storesArray["closingtime_min_tue"]       = $('#edit-closing-time-min-tue').val();
   storesArray["openingtime_type_tue"]      = $('#edit-opening-time-tue').val();
   storesArray["closingtime_type_tue"]      = $('#edit-closing-time-tue').val();

   storesArray["openingtime_hr_wen"]        = $('#edit-opening-time-hr-wen').val();
   storesArray["openingtime_min_wen"]       = $('#edit-opening-time-min-wen').val();
   storesArray["closingtime_hr_wen"]        = $('#edit-closing-time-hr-wen').val();
   storesArray["closingtime_min_wen"]       = $('#edit-closing-time-min-wen').val();
   storesArray["openingtime_type_wen"]      = $('#edit-opening-time-wen').val();
   storesArray["closingtime_type_wen"]      = $('#edit-closing-time-wen').val();

   storesArray["openingtime_hr_thu"]        = $('#edit-opening-time-hr-thu').val();
   storesArray["openingtime_min_thu"]       = $('#edit-opening-time-min-thu').val();
   storesArray["closingtime_hr_thu"]        = $('#edit-closing-time-hr-thu').val();
   storesArray["closingtime_min_thu"]       = $('#edit-closing-time-min-thu').val();
   storesArray["openingtime_type_thu"]      = $('#edit-opening-time-thu').val();
   storesArray["closingtime_type_thu"]      = $('#edit-closing-time-thu').val();

   storesArray["openingtime_hr_fri"]        = $('#edit-opening-time-hr-fri').val();
   storesArray["openingtime_min_fri"]       = $('#edit-opening-time-min-fri').val();
   storesArray["closingtime_hr_fri"]        = $('#edit-closing-time-hr-fri').val();
   storesArray["closingtime_min_fri"]       = $('#edit-closing-time-min-fri').val();
   storesArray["openingtime_type_fri"]      = $('#edit-opening-time-fri').val();
   storesArray["closingtime_type_fri"]      = $('#edit-closing-time-fri').val();

   storesArray["openingtime_hr_sat"]        = $('#edit-opening-time-hr-sat').val();
   storesArray["openingtime_min_sat"]       = $('#edit-opening-time-min-sat').val();
   storesArray["closingtime_hr_sat"]        = $('#edit-closing-time-hr-sat').val();
   storesArray["closingtime_min_sat"]       = $('#edit-closing-time-min-sat').val();
   storesArray["openingtime_type_sat"]      = $('#edit-opening-time-sat').val();
   storesArray["closingtime_type_sat"]      = $('#edit-closing-time-sat').val();

   storesArray["openingtime_hr_sun"]        = $('#edit-opening-time-hr-sun').val();
   storesArray["openingtime_min_sun"]       = $('#edit-opening-time-min-sun').val();
   storesArray["closingtime_hr_sun"]        = $('#edit-closing-time-hr-sun').val();
   storesArray["closingtime_min_sun"]       = $('#edit-closing-time-min-sun').val();
   storesArray["openingtime_type_sun"]      = $('#edit-opening-time-sun').val();
   storesArray["closingtime_type_sun"]      = $('#edit-closing-time-sun').val();
   storesArray["timezone"]                 = $('#edit-timezone').val();
   */

  storesArray["storehours"]                 = $('#edit-storehours').val();
  storesArray["status"]                = $('#edit-find-status').val();
   
  /*
   storesArray["week_mon_check"]        = $("#edit-week-mon").attr("checked") == true? "true" : "";
   storesArray["week_tue_check"]        = $("#edit-week-tue").attr("checked") == true? "true" : "";
   storesArray["week_wed_check"]        = $("#edit-week-wed").attr("checked") == true? "true" : "";
   storesArray["week_thu_check"]        = $("#edit-week-thu").attr("checked") == true? "true" : "";
   storesArray["week_fri_check"]        = $("#edit-week-fri").attr("checked") == true? "true" : "";
   storesArray["week_sat_check"]        = $("#edit-week-sat").attr("checked") == true? "true" : "";
   storesArray["week_sun_check"]        = $("#edit-week-sun").attr("checked") == true? "true" : "";
   */
   
  if(storeid >=0){
    newcategoryDialoge(storeid,categoryid);
  }




}

function newcategoryDialoge(storeid,categoryid){
  if(storeid == 0){
    reopen_dialog(600, 250,"Add Category");
  }
  else{
    reopen_dialog(600, 250,storesArray["storename"]);
  }
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/addcategory/'+storeid+'/'+categoryid,
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#edit-category-name').focus();

    //$('#edit-find-category').val(storesArray["categoryname"]);
    },
    error : function (res) {
      handleRequestError(res);
    }
  });

}

function Get_Cookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}


function storeHoursChange(day){
  if(day == 'mon'){
    if($('input[name=week_mon]').is(':checked')){
      $('#set_timings_mon').show();
    }
    else{
      $('#set_timings_mon').hide();
    }
  }

  if(day == 'tue'){
    if($('input[name=week_tue]').is(':checked')){
      $('#set_timings_tue').show();
    }
    else{
      $('#set_timings_tue').hide();
    }
  }

  if(day == 'wed'){
    if($('input[name=week_wed]').is(':checked')){
      $('#set_timings_wed').show();
    }
    else{
      $('#set_timings_wed').hide();
    }
  }

  if(day == 'thu'){
    if($('input[name=week_thu]').is(':checked')){
      $('#set_timings_thu').show();
    }
    else{
      $('#set_timings_thu').hide();
    }
  }

  if(day == 'fri'){
    if($('input[name=week_fri]').is(':checked')){
      $('#set_timings_fri').show();
    }
    else{
      $('#set_timings_fri').hide();
    }
  }

  if(day == 'sat'){
    if($('input[name=week_sat]').is(':checked')){
      $('#set_timings_sat').show();
    }
    else{
      $('#set_timings_sat').hide();
    }
  }

  if(day == 'sun'){
    if($('input[name=week_sun]').is(':checked')){
      $('#set_timings_sun').show();
    }
    else{
      $('#set_timings_sun').hide();
    }
  }
}

function importStore(){
  openDialog("importstore", 0, 500, 250, "Import Stores");
}

function importStoredialog(){
  $.ajax({
    type : "GET",
    url : baseurl+'findastore/importstores',
    cache : false,
    success : function (res) {
      $('#dialog').html(res);
      $('#importagainbut').hide();
      //$('#cancel_last').hide();
      $(".bottom_button").css("margin-left", "37%");
      $(".ui-dialog-titlebar-close").remove();
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}


function  processImport(){
  alert($('#uploadfile').val());
}

function download_template(){
  location.href = 'download';
}

function clearcategory(){
//$('#edit-st-search-category').val('Choose Category');
}

function disableScript(){
  $('#checkAll').attr('disabled', true);
	
  $('#li_category').removeAttr('onclick');
  $('#li_status').removeAttr('onclick');
  $('#li_created_date').removeAttr('onclick');
    

	
  if(($('#edit-st-search-storename') =='') && ($('#edit-st-search-category') == 'Any') &&  ($('#edit-st-search-status') == 'Any') && ($('#edit-st-search-date') == '')){
    $('#li_storename').removeAttr('onclick');
    $('#li_store_category').removeAttr('onclick');
    $('#li_storestatus').removeAttr('onclick');
    $('#li_store_created_date').removeAttr('onclick');
    $('#filter_store_created_date').removeAttr('onclick');
    $('#filter_store_status').removeAttr('onclick');
    $('#filter_store_category').removeAttr('onclick');
    $('#filter_storename').removeAttr('onclick');
  }
	
  $('#edit-activate').attr('disabled', 'true');
  $('#edit-deactivate').attr('disabled', 'true');

  //$("div[class='filter-action']").removeAttr('onclick');
   
  	   	   
  if(($('#edit-ct-search-categoryname') =='') && ($('#edit-ct-search-status') == 'Any') &&  ($('#edit-ct-search-date') == '')){
    $('#filter_category').removeAttr('onclick');
    $('#filter_created_date').removeAttr('onclick');
    $('#filter_status').removeAttr('onclick');
  }
	
	
	   
}


function enableScript(){

  $('#checkAll').attr('disabled', false);
  $('#edit-activate').attr('disabled', false);
  $('#edit-deactivate').attr('disabled', false);

/*
  $("#li_category").attr("onclick","sortColumn('categories','categoryName');");
  $('#li_status').attr('onclick',"sortColumn('categories','status');");
  $('#li_created_date').attr('onclick',"sortColumn('categories','insertDate');");
  
  $("#li_storename").attr("onclick","sortColumn('stores','storeName');");
  $("#li_store_category").attr("onclick","sortColumn('stores','categoryName');clearcategory();");
  $("#li_storestatus").attr("onclick","sortColumn('stores','status');");
  $("#li_store_created_date").attr("onclick","sortColumn('stores','insertDate');");
*/

}

function disable_scoreview_button(){
//styleStatusMessages("There is no stores associated with this category", "statusMessage", "failure");
}

function enable_scoreview_button(){
  $('#statusMessage').hide();
}

var json_map_data = "";
var invalid_address = 0;
//Function to validate the address using APIs
function validate_address () {
	
  //$("#ShowonMap").hide();

  
  
  $("#ShowonMap").show();
  var storename = $.trim($('#edit-find-store-name').val());
  var address1 = $.trim($('#edit-find-address1').val());
  var address2 = $.trim($('#edit-find-address2').val());
  var zipcode = $.trim($('#edit-find-zipcode').val());

  var state = $.trim($('#edit-find-state').val());
  var usstates =$.trim($('#edit-find-usstates').val());
  var selectedstate="";
  
  
  var city = $.trim($('#edit-find-city').val());
  var country = $("#edit-find-country").val();  
  var phoneno = $("#edit-work-phone").val();
  var statuscheck = $("#edit-find-status").val();

  var selected = $("#edit-find-country option:selected");
  if(selected.val() != -1)
  {
    var countryname=selected.text();
  }
  

  
  
  var lines = 10;
  var str = "";
		

  if(country == -1){
    if(str!="")
      str+= "<br/>";
    str += "Please select the country";
    lines+=18;
  }

  if(address1 == ""){
    if(str!="")
      str+= "<br/>";
    str += "Please enter the Address Line 1";
    lines+=18;
  }

  if(city == ""){
    if(str!="")
      str+= "<br/>";
    str += "Please enter the City";
    lines+=18;
  }

  //if(country == "United States") {
  if(country == "US") {
    if(usstates == "")
    {
      if(str!="") str+= "<br/>";
      str += "Please select the State/Province";
      lines+=18;
    }
  }

  //if(country != "United States") {
  if(country != "US") {
    if(state == "")
    {
      if(str!="") str+= "<br/>";
      str += "Please enter the State/Province";
      lines+=18;
    }
  }

  if(country == "US") {
    if(usstates != "")
    {
      selectedstate=$.trim($('#edit-find-usstates').val());
        
    }
  }

  if(country != "US") {
    if(state != "")
    {
      selectedstate=$.trim($('#edit-find-state').val());
    }
  }

             
  if(str!=""){
    resize_dialog(680, 400 + lines);
    //styleStatusMessages(str, "statusMessage", "failure");
    openAlertDialog(300, 130, 'Error', 'Please enter a valid Address');
    $('#dialog').scrollTop(0);
    return;
  }
  $("#statusMessage").html("");
  $("#statusMessage").hide();
	 
  //Call to AJAX to validate the address using API
  //var addressLine = address1 + address2;
  $.ajax({
    type : "POST",
    //data : "address1=" + encodeURIComponent(address1) + "&address2=" + encodeURIComponent(address2) + "&postalCode=" + encodeURIComponent(zipcode) + "&state=" + encodeURIComponent(state) + "&city=" + encodeURIComponent(city) + "&country=" + encodeURIComponent(countryname)+"&usstates=" + encodeURIComponent(usstates),
    data : "address1=" + encodeURIComponent(address1) + "&address2=" + encodeURIComponent(address2) + "&postalCode=" + encodeURIComponent(zipcode) + "&state=" + encodeURIComponent(selectedstate) + "&city=" + encodeURIComponent(city) + "&country=" + encodeURIComponent(countryname),
    url : baseurl+'findastore/address/validate',
    cache : false,
    success : function (res) {
      eval("var json_map=" + res);
         

        // if (typeof json_map.FindAddressLocation.latitude == "undefined") {
	 if ((json_map.FindAddressLocation.latitude == "") ||(json_map.FindAddressLocation == "Geo location is not found")||(json_map.FindAddressLocation.latitude == null) ||(json_map.FindAddressLocation.latitude == "undefined"))  {
         // if (typeof json_map.FindAddressLocation.latitude != "undefined" && json_map.FindAddressLocation.longitude == "undefined"){
         //$("#ShowonMap").hide();
         //this is added to show te showon map link by default         
          $("#ShowonMap").show();
          invalid_address = 1;
          resize_dialog(680, 470 + 18);          
          openAlertDialog(300, 130, 'Error', 'Please enter a valid address');
          $('#dialog').scrollTop(0);
          $('#ShowonMap').destroy;
          return;
        }
        else {
          invalid_address = 0;

          $("#ShowonMap").show();

          json_map_data = res;

          show_address_on_map();
          
          $('#ShowonMap').destroy;


      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

